﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Silverlight.Library;

namespace FRL.DebtChased.AgentEditor
{

	/// <summary>
	/// Declaration of CountryCoverageWnd class
	/// Child window allowing user to select which countries
	/// the agent operates in.
	/// </summary>
	public partial class CountryCoverageWnd : ChildWindow
	{

		/////////////////////////////////////////////////////////////
		// Attributes

		private List<string> m_countryCoverage;

		/////////////////////////////////////////////////////////////
		// Construction

		public CountryCoverageWnd(List<string> countryCoverage)
		{

			// Initialize member variables
			m_countryCoverage = countryCoverage;

			// Load xaml resources
			InitializeComponent();

			// Load the controls
			foreach (var di in CachedData.Singleton.CountryMap)
			{
				if (m_countryCoverage.Contains(di.Key))
					lstSelected.Items.Add( new ComboBoxObject(di.Value,0,di) );
				else
					lstAvailable.Items.Add( new ComboBoxObject(di.Value, 0, di) );
			}

			// Setup controls
			_setupControls();

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

		private void OKButton_Click(object sender, RoutedEventArgs e)
		{

			// Update code list
			m_countryCoverage.Clear();
			foreach (object ob in lstSelected.Items)
			{
				ComboBoxObject cbo = ob as ComboBoxObject;
				KeyValuePair<string, string> kvp = (KeyValuePair<string, string>)cbo.Tag;
				m_countryCoverage.Add(kvp.Key);
			}

			// Close dialog
			this.DialogResult = true;

		}

		private void CancelButton_Click(object sender, RoutedEventArgs e)
		{

			// Close dialog
			this.DialogResult = false;

		}

		private void butAdd_Click(object sender, RoutedEventArgs e)
		{

			// Swap over selected item
			while (lstAvailable.SelectedItems.Count > 0)
			{
				Object ob = lstAvailable.SelectedItems[0];
				lstAvailable.Items.Remove(ob);
				lstSelected.Items.Add(ob);				
			}

			// Setup controls
			_setupControls();

		}

		private void butRemove_Click(object sender, RoutedEventArgs e)
		{

			// Swap over selected item
			while (lstSelected.SelectedItems.Count > 0)
			{
				Object ob = lstSelected.SelectedItems[0];
				lstSelected.Items.Remove(ob);
				lstAvailable.Items.Add(ob);
			}

			// Setup controls
			_setupControls();

		}

		private void butRemoveAll_Click(object sender, RoutedEventArgs e)
		{

			// Swap over items
			while (lstSelected.Items.Count > 0)
			{
				Object ob = lstSelected.Items[0];
				lstSelected.Items.Remove(ob);
				lstAvailable.Items.Add(ob);
			}

			// Setup controls
			_setupControls();

		}

		private void lstAvailable_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// Setup controls
			_setupControls();

		}

		private void lstSelected_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// Setup controls
			_setupControls();

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private void _setupControls()
		{

			// Enable/disable controls
			butAdd.IsEnabled = (lstAvailable.SelectedItems.Count > 0);
			butRemove.IsEnabled = (lstSelected.SelectedItems.Count > 0);
			butRemoveAll.IsEnabled = (lstSelected.Items.Count>0);
			OKButton.IsEnabled = (lstSelected.Items.Count > 0);

		}

	}

}

